using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StandardLevelTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StandardLevelTable objects.
    /// </summary>
    public class StandardLevelTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StandardLevelTable from the underlying datasource.
        /// </summary>
        /// <param name="standardLevelKey">The unique StandardLevelKey of the StandardLevelTable in the database.</param>
        /// <returns>An StandardLevelTable when the StandardLevelKey was found in the database, or null otherwise.</returns>
        public static StandardLevelTable GetItem(int standardLevelKey)
        {
            StandardLevelTable myStandardLevelTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStandardLevelTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@standardLevelKey", standardLevelKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStandardLevelTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStandardLevelTable;
            }
        }

        /// <summary>
        /// Returns a list with StandardLevelTable objects.
        /// </summary>
        /// <returns>A generics List with the StandardLevelTable objects.</returns>
        public static StandardLevelTableList GetList(int schoolKey, int schoolMajorKey)
        {
            StandardLevelTableList tempList = new StandardLevelTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStandardLevelTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", schoolKey);
                    myCommand.Parameters.AddWithValue("@schoolMajorKey", schoolMajorKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StandardLevelTable in the database.
        /// </summary>
        /// <param name="myStandardLevelTable">The StandardLevelTable instance to save.</param>
        /// <returns>The new StandardLevelKey if the StandardLevelTable is new in the database or the existing StandardLevelKey when an item was updated.</returns>
        public static int Save(StandardLevelTable myStandardLevelTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStandardLevelTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStandardLevelTable.StandardLevelKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", myStandardLevelTable.StandardLevelKey);
                    }
                    if (myStandardLevelTable.SchoolKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myStandardLevelTable.SchoolKey);
                    }
                    if (myStandardLevelTable.SchoolMajorKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorKey", myStandardLevelTable.SchoolMajorKey);
                    }
                    if (string.IsNullOrEmpty(myStandardLevelTable.StandardLevelCode))
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelCode", myStandardLevelTable.StandardLevelCode);
                    }
                    if (string.IsNullOrEmpty(myStandardLevelTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myStandardLevelTable.Description);
                    }
                    if (myStandardLevelTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myStandardLevelTable.Status);
                    }

                    //if (myStandardLevelTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStandardLevelTable.CreatedDateTime);
                    //}
                    //if (myStandardLevelTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStandardLevelTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StandardLevelTable from the database.
        /// </summary>
        /// <param name="standardLevelKey">The StandardLevelKey of the StandardLevelTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int standardLevelKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStandardLevelTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@standardLevelKey", standardLevelKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StandardLevelTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StandardLevelTable FillDataRecord(IDataRecord myDataRecord)
        {
            StandardLevelTable myStandardLevelTable = new StandardLevelTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelKey")))
            {
                myStandardLevelTable.StandardLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandardLevelKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myStandardLevelTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolMajorKey")))
            {
                myStandardLevelTable.SchoolMajorKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolMajorKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelCode")))
            {
                myStandardLevelTable.StandardLevelCode = myDataRecord.GetString(myDataRecord.GetOrdinal("StandardLevelCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myStandardLevelTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStandardLevelTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStandardLevelTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStandardLevelTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStandardLevelTable;
        }
    }

}
